const { BASE_URL } = require("../../../config");
const { User, DynamicContent } = require("../../../models");
const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId;

// 获取用户【动态】
async function getDynamic(io, socket, data, callback) {
    if (data == undefined || data == null) {
        console.log('getDynamic data 错误', data);
        return;
    }

    let doc_user = await User.findById(data)
    .populate({
        path: 'DynamicList',
        populate: [
            { path: 'liked_users', select: 'nickname -_id' },
            {
                path: 'comment',
                select: '-_id',
                populate: { path: 'user', select: 'nickname -_id' }
            }
        ] 
    })
    
    if (doc_user == null) {
        console.log('getDynamic-doc_user 为空');
        callback({ status_code: 200, info: 'getDynamic-doc_user 为空'});
        return;
    }

    let userInfo = {
        _id: doc_user._id,
        phone: doc_user.phone,
        upvote: doc_user.upvote,
        nickname: doc_user.nickname,
        headImgUrl: doc_user.headImgUrl,
        cover: doc_user.cover
    }

    // 处理填充后的字段，不能直接改 doc_user.DynamicList，否则东西还是原值
    let res = doc_user.DynamicList.map(element => {
        let obj = {
            _id        : element._id,
            imageUrls  : element.imageUrls.map(item => BASE_URL + item),
            comment    : element.comment.map(({ user, content, tiem}) => { return { content, tiem, user: user.nickname }}),
            liked_users: element.liked_users.map(item => item.nickname),
            text       : element.text,
            time       : element.time
        }
        return obj;
    })

    callback({ 
        status_code: 200, 
        userInfo, 
        dynamicList: res,
    });
}

// 处理【动态】被其他人点赞
async function handleLike_dynamic(io, socket, data, callback) {
    if (data == null || data == undefined) {
        console.log('handleLike_dynamic: data数据错误', data);
        return;
    }

    let { dynamic_id, liked_user_id } = data;
    let doc_dynamic = await DynamicContent.findById(dynamic_id);

    if ((doc_dynamic.liked_users.findIndex(item => item.toString() == liked_user_id)) != -1) {
        callback({ status_code: 404, info: '已经点赞过了', data: 0 });
        return;
    }
    
    doc_dynamic.liked_users.push(new ObjectId(liked_user_id));

    let doc_user = await User.findById(liked_user_id);

    doc_dynamic.save()
    .then(() => callback({ 
        status_code: 200, 
        info: '点赞成功', 
        data: { 
            dynamic_id,
            user_nickname: doc_user.nickname,
        } 
    }))
    .catch(err => callback({ status_code: 400, info: err, data: 0 }));
}

// 处理【动态】被其他人评论
async function handleSend_comment_dynamic(io, socket, data, callback) {
    if (data == null || data == undefined) {
        console.log('handleSend_comment_dynamic: data数据错误', data);
        return;
    }

    let { dynamic_id, comment_user_id,  comment} = data;
    let doc_dynamic = await DynamicContent.findById(dynamic_id);
    let doc_user = await User.findById(comment_user_id);

    doc_dynamic.comment.push({
        user: new ObjectId(comment_user_id),
        content: comment,
    });

    doc_dynamic.save()
    .then(() => callback({ 
        status_code: 200, 
        info: '评论成功', 
        data: {
            dynamic_id,
            content: comment,
            user: doc_user.nickname
        } 
    }))
    .catch(err => callback({ status_code: 400, info: err, data: 0 }));
}

module.exports = {
    getDynamic,
    handleLike_dynamic,
    handleSend_comment_dynamic,
};